package de.lns.ui.keyinput;

import java.nio.FloatBuffer;

import de.lns.entities.turrets.FireShotTurret;
import de.lns.entities.turrets.ImplosionShotTurret;
import de.lns.entities.turrets.MagicalShotTurret;
import de.lns.entities.turrets.PhysicalShotTurret;
import de.lns.entities.turrets.PoisonShotTurret;
import de.lns.level.Level;
import de.lns.render.Drawable;
import de.lns.render.Model;
import de.lns.render.ModelFactory;
import de.lns.render.Renderer;
import de.lns.render.TextureLoader;
import de.lns.tick.Input;
import de.lns.tick.KeyEvent;
import de.lns.tick.MouseEvent;
/**
 * 
 * @author Jascha
 *
 */
public class Inputhandler implements Input, Drawable{
	
	private MouseEvent mouse;
	private KeyEvent key;
	private static Inputhandler instance;
	private FloatBuffer mesh;
	private FloatBuffer mapping;
	private Model model;
	private int texture;
	private double xPos;
	private double yPos;
	
	/**
	 * 
	 * @return singelton so keyinputs can only get handled once
	 */
	public static Inputhandler getInstance(){
		if(instance==null){
			instance= new Inputhandler();
		}
		return instance;
	}
	private Inputhandler(){
		float x = Level.getMap().getMesh().get(2)*Renderer.getMapRatio();
		float width = Renderer.getScreenWidth()-x;
		float height = Renderer.getScreenHeight();
		mesh = ModelFactory.genRectMesh(width, height, width/2, height/2);
		mapping = ModelFactory.genRectMap();
		
		texture = TextureLoader.loadTexture("Overlay+Menu/Orientation.png");
		
		xPos = x;
		yPos = 0;
		
		model = ModelFactory.genModelSimple(this);
	}
	@Override
	public void mouse(MouseEvent m) {
		this.mouse=m;		
	}

	@Override
	public void key(KeyEvent k) {
		this.key=k;
		placeturret(k.key);
		
	}
	
	/**
	 * 
	 * @param key the key you need to press for a specific turret
	 */
	private void placeturret(char key){
		switch(key){
		case('1'):
			Level.tryplaceTurret(new ImplosionShotTurret(mouse.mouseX/Renderer.getMapRatio(),mouse.mouseY/Renderer.getMapRatio()));
			break;
		case('2'):
			Level.tryplaceTurret(new FireShotTurret(mouse.mouseX/Renderer.getMapRatio(),mouse.mouseY/Renderer.getMapRatio()));
			break;
		case('3'):
			Level.tryplaceTurret(new MagicalShotTurret(mouse.mouseX/Renderer.getMapRatio(),mouse.mouseY/Renderer.getMapRatio()));
			break;
		case('4'):
			Level.tryplaceTurret(new PhysicalShotTurret(mouse.mouseX/Renderer.getMapRatio(),mouse.mouseY/Renderer.getMapRatio()));
			break;
		case('5'):
			Level.tryplaceTurret(new PoisonShotTurret(mouse.mouseX/Renderer.getMapRatio(),mouse.mouseY/Renderer.getMapRatio()));
			break;
		case('6'):
			break;
		case('7'):
			break;
		case('8'):
			break;
		case('9'):
			break;
			
		}
	}
	@Override
	public Model model() {
		return model;
	}
	@Override
	public FloatBuffer getMesh() {
		return mesh;
	}
	@Override
	public FloatBuffer getMapping() {
		return mapping;
	}
	@Override
	public int getTextureID() {
		return texture;
	}
	@Override
	public boolean alignRel() {
		return false;
	}
	@Override
	public double getScale() {
		return 1.;
	}
	@Override
	public double getPosX() {
		return xPos;
	}
	@Override
	public double getPosY() {
		return yPos;
	}
	@Override
	public double getPosZ() {
		return 1;
	}
	@Override
	public double getDirection() {
		return 0;
	}

}
